/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Window;

import jplay.*;
import java.awt.DisplayMode;

import java.util.HashMap;
import java.util.Map;

import Controladores.ControladorJanela;
import Sueca.Carta;
import java.awt.Point;

/**
 *
 * @author Administrador
 */
public class Janela {

    Window w = new Window(800,600);

    Mouse mouse = w.getMouse();


    private static Janela instance;

    HashMap hash = new HashMap();

    HashMap ordem = new HashMap();

    HashMap mao = new HashMap();


    private Janela(){

    }
/*
    private HashMap getHash(){
        return hash;
    }

    public void setHash(HashMap map){
        this.hash =  map;
    }
*/
    public static Janela getInstance(){
        if(instance == null){
            instance = new Janela();
        }
        return instance;
    }

     //cria janela da aplicação
     public void iniciajanela(){

        //Seta o fundo da tela.
        GameImage backGround = new GameImage("image/feltro.jpg");
        backGround.draw();

        w.update();

/*
        Sprite ce00 = new Sprite("image/back-blue-150-3.png");
        ce00.x = 29;
        ce00.y = 184;

        hash.put(0, ce00);

        Sprite Caux;

        Caux = (Sprite) hash.get(0);
        Caux.draw();
*/
//        inicializaCartas();
//        inicializaOrdem();
//        distribuirCartas (Cartas);

//        w.update();

    }

   public void inicializaCartas(){


        //inicialização das imagens das cartas
        //ouro
        Sprite c00 = new Sprite("image/diamonds-2-150.png");
        Sprite c01 = new Sprite("image/diamonds-3-150.png");
        Sprite c02 = new Sprite("image/diamonds-4-150.png");
        Sprite c03 = new Sprite("image/diamonds-5-150.png");
        Sprite c04 = new Sprite("image/diamonds-6-150.png");
        Sprite c05 = new Sprite("image/diamonds-7-150.png");
        Sprite c06 = new Sprite("image/diamonds-j-150.png");
        Sprite c07 = new Sprite("image/diamonds-q-150.png");
        Sprite c08 = new Sprite("image/diamonds-k-150.png");
        Sprite c09 = new Sprite("image/diamonds-a-150.png");

        //paus
        Sprite c10 = new Sprite("image/clubs-2-150.png");
        Sprite c11 = new Sprite("image/clubs-3-150.png");
        Sprite c12 = new Sprite("image/clubs-4-150.png");
        Sprite c13 = new Sprite("image/clubs-5-150.png");
        Sprite c14 = new Sprite("image/clubs-6-150.png");
        Sprite c15 = new Sprite("image/clubs-7-150.png");
        Sprite c16 = new Sprite("image/clubs-j-150.png");
        Sprite c17 = new Sprite("image/clubs-q-150.png");
        Sprite c18 = new Sprite("image/clubs-k-150.png");
        Sprite c19 = new Sprite("image/clubs-a-150.png");

        //copas
        Sprite c20 = new Sprite("image/hearts-2-150.png");
        Sprite c21 = new Sprite("image/hearts-3-150.png");
        Sprite c22 = new Sprite("image/hearts-4-150.png");
        Sprite c23 = new Sprite("image/hearts-5-150.png");
        Sprite c24 = new Sprite("image/hearts-6-150.png");
        Sprite c25 = new Sprite("image/hearts-7-150.png");
        Sprite c26 = new Sprite("image/hearts-j-150.png");
        Sprite c27 = new Sprite("image/hearts-q-150.png");
        Sprite c28 = new Sprite("image/hearts-k-150.png");
        Sprite c29 = new Sprite("image/hearts-a-150.png");

        //espada
        Sprite c30 = new Sprite("image/spades-2-150.png");
        Sprite c31 = new Sprite("image/spades-3-150.png");
        Sprite c32 = new Sprite("image/spades-4-150.png");
        Sprite c33 = new Sprite("image/spades-5-150.png");
        Sprite c34 = new Sprite("image/spades-6-150.png");
        Sprite c35 = new Sprite("image/spades-7-150.png");
        Sprite c36 = new Sprite("image/spades-j-150.png");
        Sprite c37 = new Sprite("image/spades-q-150.png");
        Sprite c38 = new Sprite("image/spades-k-150.png");
        Sprite c39 = new Sprite("image/spades-a-150.png");

        //inicializando posição Y de todas as cartas
        //ouro
        c00.y = 494;
        c01.y = 494;
        c02.y = 494;
        c03.y = 494;
        c04.y = 494;
        c05.y = 494;
        c06.y = 494;
        c07.y = 494;
        c08.y = 494;
        c09.y = 494;

        //paus
        c10.y = 494;
        c11.y = 494;
        c12.y = 494;
        c13.y = 494;
        c14.y = 494;
        c15.y = 494;
        c16.y = 494;
        c17.y = 494;
        c18.y = 494;
        c19.y = 494;

        //copas
        c20.y = 494;
        c21.y = 494;
        c22.y = 494;
        c23.y = 494;
        c24.y = 494;
        c25.y = 494;
        c26.y = 494;
        c27.y = 494;
        c28.y = 494;
        c29.y = 494;

        //espada
        c30.y = 494;
        c31.y = 494;
        c32.y = 494;
        c33.y = 494;
        c34.y = 494;
        c35.y = 494;
        c36.y = 494;
        c37.y = 494;
        c38.y = 494;
        c39.y = 494;


        //coloca no mapa o sprite da carta e o seu id
        //ouro
        hash.put(0, c00);
        hash.put(1, c01);
        hash.put(2, c02);
        hash.put(3, c03);
        hash.put(4, c04);
        hash.put(5, c05);
        hash.put(6, c06);
        hash.put(7, c07);
        hash.put(8, c08);
        hash.put(9, c09);

        //paus
        hash.put(10, c10);
        hash.put(11, c11);
        hash.put(12, c12);
        hash.put(13, c13);
        hash.put(14, c14);
        hash.put(15, c15);
        hash.put(16, c16);
        hash.put(17, c17);
        hash.put(18, c18);
        hash.put(19, c19);

        //copas
        hash.put(20, c20);
        hash.put(21, c21);
        hash.put(22, c22);
        hash.put(23, c23);
        hash.put(24, c24);
        hash.put(25, c25);
        hash.put(26, c26);
        hash.put(27, c27);
        hash.put(28, c28);
        hash.put(29, c29);

        //espada
        hash.put(30, c30);
        hash.put(31, c31);
        hash.put(32, c32);
        hash.put(33, c33);
        hash.put(34, c34);
        hash.put(35, c35);
        hash.put(36, c36);
        hash.put(37, c37);
        hash.put(38, c38);
        hash.put(39, c39);

        //Inicializar "costas" das cartas
        //esquerda
        Sprite ce00 = new Sprite("image/back-blue-150-3.png");
        Sprite ce01 = new Sprite("image/back-blue-150-3.png");
        Sprite ce02 = new Sprite("image/back-blue-150-3.png");
        Sprite ce03 = new Sprite("image/back-blue-150-3.png");
        Sprite ce04 = new Sprite("image/back-blue-150-3.png");
        Sprite ce05 = new Sprite("image/back-blue-150-3.png");
        Sprite ce06 = new Sprite("image/back-blue-150-3.png");
        Sprite ce07 = new Sprite("image/back-blue-150-3.png");
        Sprite ce08 = new Sprite("image/back-blue-150-3.png");
        Sprite ce09 = new Sprite("image/back-blue-150-3.png");

        //direita
        Sprite cd00 = new Sprite("image/back-blue-150-3.png");
        Sprite cd01 = new Sprite("image/back-blue-150-3.png");
        Sprite cd02 = new Sprite("image/back-blue-150-3.png");
        Sprite cd03 = new Sprite("image/back-blue-150-3.png");
        Sprite cd04 = new Sprite("image/back-blue-150-3.png");
        Sprite cd05 = new Sprite("image/back-blue-150-3.png");
        Sprite cd06 = new Sprite("image/back-blue-150-3.png");
        Sprite cd07 = new Sprite("image/back-blue-150-3.png");
        Sprite cd08 = new Sprite("image/back-blue-150-3.png");
        Sprite cd09 = new Sprite("image/back-blue-150-3.png");

        //cima
        Sprite cc00 = new Sprite("image/back-blue-150-3.png");
        Sprite cc01 = new Sprite("image/back-blue-150-3.png");
        Sprite cc02 = new Sprite("image/back-blue-150-3.png");
        Sprite cc03 = new Sprite("image/back-blue-150-3.png");
        Sprite cc04 = new Sprite("image/back-blue-150-3.png");
        Sprite cc05 = new Sprite("image/back-blue-150-3.png");
        Sprite cc06 = new Sprite("image/back-blue-150-3.png");
        Sprite cc07 = new Sprite("image/back-blue-150-3.png");
        Sprite cc08 = new Sprite("image/back-blue-150-3.png");
        Sprite cc09 = new Sprite("image/back-blue-150-3.png");

        //inicialização da posição das cartas viradas
        //esquerda
        ce00.x = 29;
        ce01.x = 29;
        ce02.x = 29;
        ce03.x = 29;
        ce04.x = 29;
        ce05.x = 29;
        ce06.x = 29;
        ce07.x = 29;
        ce08.x = 29;
        ce09.x = 29;

        ce00.y = 184;
        ce01.y = 199;
        ce02.y = 214;
        ce03.y = 229;
        ce04.y = 244;
        ce05.y = 259;
        ce06.y = 274;
        ce07.y = 289;
        ce08.y = 304;
        ce09.y = 319;

        //direita
        cd00.x = 700;
        cd01.x = 700;
        cd02.x = 700;
        cd03.x = 700;
        cd04.x = 700;
        cd05.x = 700;
        cd06.x = 700;
        cd07.x = 700;
        cd08.x = 700;
        cd09.x = 700;

        cd00.y = 184;
        cd01.y = 199;
        cd02.y = 214;
        cd03.y = 229;
        cd04.y = 244;
        cd05.y = 259;
        cd06.y = 274;
        cd07.y = 289;
        cd08.y = 304;
        cd09.y = 319;

        //cima
        cc00.x = 297;
        cc01.x = 312;
        cc02.x = 327;
        cc03.x = 342;
        cc04.x = 357;
        cc05.x = 372;
        cc06.x = 387;
        cc07.x = 402;
        cc08.x = 417;
        cc09.x = 432;

        cc00.y = 10;
        cc01.y = 10;
        cc02.y = 10;
        cc03.y = 10;
        cc04.y = 10;
        cc05.y = 10;
        cc06.y = 10;
        cc07.y = 10;
        cc08.y = 10;
        cc09.y = 10;

        //mapeando
        hash.put(40, ce00);
        hash.put(41, ce01);
        hash.put(42, ce02);
        hash.put(43, ce03);
        hash.put(44, ce04);
        hash.put(45, ce05);
        hash.put(46, ce06);
        hash.put(47, ce07);
        hash.put(48, ce08);
        hash.put(49, ce09);

        hash.put(50, cd00);
        hash.put(51, cd01);
        hash.put(52, cd02);
        hash.put(53, cd03);
        hash.put(54, cd04);
        hash.put(55, cd05);
        hash.put(56, cd06);
        hash.put(57, cd07);
        hash.put(58, cd08);
        hash.put(59, cd09);

        hash.put(60, cc00);
        hash.put(61, cc01);
        hash.put(62, cc02);
        hash.put(63, cc03);
        hash.put(64, cc04);
        hash.put(65, cc05);
        hash.put(66, cc06);
        hash.put(67, cc07);
        hash.put(68, cc08);
        hash.put(69, cc09);
    }

    public void inicializaOrdem(){
        Integer pos00 = 297;
        Integer pos01 = 312;
        Integer pos02 = 327;
        Integer pos03 = 342;
        Integer pos04 = 357;
        Integer pos05 = 372;
        Integer pos06 = 387;
        Integer pos07 = 402;
        Integer pos08 = 417;
        Integer pos09 = 432;

        ordem.put(0, pos00);
        ordem.put(1, pos01);
        ordem.put(2, pos02);
        ordem.put(3, pos03);
        ordem.put(4, pos04);
        ordem.put(5, pos05);
        ordem.put(6, pos06);
        ordem.put(7, pos07);
        ordem.put(8, pos08);
        ordem.put(9, pos09);
    }

    public void distribuirCartas (int[] maojogador){

            //Distribuição de cartas da mão do jogador */

            inicializaCartas();

            Sprite priCarta;
            Sprite segCarta;
            Sprite terCarta;
            Sprite quaCarta;
            Sprite quiCarta;
            Sprite sexCarta;
            Sprite setCarta;
            Sprite oitCarta;
            Sprite novCarta;
            Sprite decCarta;

            Carta aux;

            priCarta = (Sprite) hash.get(maojogador[0]);
            priCarta.x = 297;
            priCarta.draw();
            mao.put(0,priCarta);

            segCarta = (Sprite) hash.get(maojogador[1]);
            segCarta.x = 312;
            segCarta.draw();
            mao.put(1,segCarta);

            terCarta = (Sprite) hash.get(maojogador[2]);
            terCarta.x = 327;
            terCarta.draw();
            mao.put(2,terCarta);

            quaCarta = (Sprite) hash.get(maojogador[3]);
            quaCarta.x = 342;
            quaCarta.draw();
            mao.put(3,quaCarta);

            quiCarta = (Sprite) hash.get(maojogador[4]);
            quiCarta.x = 357;
            quiCarta.draw();
            mao.put(4,quiCarta);

            sexCarta = (Sprite) hash.get(maojogador[5]);
            sexCarta.x = 372;
            sexCarta.draw();
            mao.put(5,sexCarta);

            setCarta = (Sprite) hash.get(maojogador[6]);
            setCarta.x = 387;
            setCarta.draw();
            mao.put(6,setCarta);

            oitCarta = (Sprite) hash.get(maojogador[7]);
            oitCarta.x = 402;
            oitCarta.draw();
            mao.put(7,oitCarta);

            novCarta = (Sprite) hash.get(maojogador[8]);
            novCarta.x = 417;
            novCarta.draw();
            mao.put(8,novCarta);

            decCarta = (Sprite) hash.get(maojogador[9]);
            decCarta.x = 432;
            decCarta.draw();
            mao.put(9,decCarta);
/*
            Cartas.put(0, priCarta);
            Cartas.put(1, segCarta);
            Cartas.put(2, terCarta);
            Cartas.put(3, quaCarta);
            Cartas.put(4, quiCarta);
            Cartas.put(5, sexCarta);
            Cartas.put(6, setCarta);
            Cartas.put(7, oitCarta);
            Cartas.put(8, novCarta);
            Cartas.put(9, decCarta);
*/
            Sprite priCartaE;
            Sprite segCartaE;
            Sprite terCartaE;
            Sprite quaCartaE;
            Sprite quiCartaE;
            Sprite sexCartaE;
            Sprite setCartaE;
            Sprite oitCartaE;
            Sprite novCartaE;
            Sprite decCartaE;

            Sprite priCartaD;
            Sprite segCartaD;
            Sprite terCartaD;
            Sprite quaCartaD;
            Sprite quiCartaD;
            Sprite sexCartaD;
            Sprite setCartaD;
            Sprite oitCartaD;
            Sprite novCartaD;
            Sprite decCartaD;

            Sprite priCartaC;
            Sprite segCartaC;
            Sprite terCartaC;
            Sprite quaCartaC;
            Sprite quiCartaC;
            Sprite sexCartaC;
            Sprite setCartaC;
            Sprite oitCartaC;
            Sprite novCartaC;
            Sprite decCartaC;

            priCartaE = (Sprite) hash.get(40);
            priCartaE.draw();
            segCartaE = (Sprite) hash.get(41);
            segCartaE.draw();
            terCartaE = (Sprite) hash.get(42);
            terCartaE.draw();
            quaCartaE = (Sprite) hash.get(43);
            quaCartaE.draw();
            quiCartaE = (Sprite) hash.get(44);
            quiCartaE.draw();
            sexCartaE = (Sprite) hash.get(45);
            sexCartaE.draw();
            setCartaE = (Sprite) hash.get(46);
            setCartaE.draw();
            oitCartaE = (Sprite) hash.get(47);
            oitCartaE.draw();
            novCartaE = (Sprite) hash.get(48);
            novCartaE.draw();
            decCartaE = (Sprite) hash.get(49);
            decCartaE.draw();

            priCartaD = (Sprite) hash.get(50);
            priCartaD.draw();
            segCartaD = (Sprite) hash.get(51);
            segCartaD.draw();
            terCartaD = (Sprite) hash.get(52);
            terCartaD.draw();
            quaCartaD = (Sprite) hash.get(53);
            quaCartaD.draw();
            quiCartaD = (Sprite) hash.get(54);
            quiCartaD.draw();
            sexCartaD = (Sprite) hash.get(55);
            sexCartaD.draw();
            setCartaD = (Sprite) hash.get(56);
            setCartaD.draw();
            oitCartaD = (Sprite) hash.get(57);
            oitCartaD.draw();
            novCartaD = (Sprite) hash.get(58);
            novCartaD.draw();
            decCartaD = (Sprite) hash.get(59);
            decCartaD.draw();

            priCartaC = (Sprite) hash.get(60);
            priCartaC.draw();
            segCartaC = (Sprite) hash.get(61);
            segCartaC.draw();
            terCartaC = (Sprite) hash.get(62);
            terCartaC.draw();
            quaCartaC = (Sprite) hash.get(63);
            quaCartaC.draw();
            quiCartaC = (Sprite) hash.get(64);
            quiCartaC.draw();
            sexCartaC = (Sprite) hash.get(65);
            sexCartaC.draw();
            setCartaC = (Sprite) hash.get(66);
            setCartaC.draw();
            oitCartaC = (Sprite) hash.get(67);
            oitCartaC.draw();
            novCartaC = (Sprite) hash.get(68);
            novCartaC.draw();
            decCartaC = (Sprite) hash.get(69);
            decCartaC.draw();

        w.update();
    }

    public void pintarCartas(int[] maoJogadorJ0, int[] maoJogadorJ1){

    }

    public int jogaCarta(int[] maoJogador){

        //Sprite carta;
        Point ponto;
        int x;
        int y;

        while (true){
            //pinta cartas
            if( mouse.isLeftButtonPressed() ){
                ponto = mouse.getPosition();

                x = ponto.x;
                y = ponto.y;

                if ((y >= 494)&&(y <= 590)){
                    if ((x > (Integer) ordem.get(0))&&(x < (Integer) ordem.get(1))){
                      // carta = (Sprite) mao.get(0);
                       return maoJogador[0];
                       // centralizaCarta(carta);
                    }else{
                        if ((x > (Integer) ordem.get(1))&&(x < (Integer) ordem.get(2))){
                           return maoJogador[1];
                           // carta = (Sprite) mao.get(1);
                           // centralizaCarta(carta);
                        }else{
                            if ((x > (Integer) ordem.get(2))&&(x < (Integer) ordem.get(3))){
                               return maoJogador[2];
                               // carta = (Sprite) mao.get(2);
                               // centralizaCarta(carta);
                            }else{
                                if ((x > (Integer) ordem.get(3))&&(x < (Integer) ordem.get(4))){
                                    return maoJogador[3];
                                   // carta = (Sprite) mao.get(3);
                                   // centralizaCarta(carta);
                                }else{
                                    if ((x > (Integer) ordem.get(4))&&(x < (Integer) ordem.get(5))){
                                        return maoJogador[4];
                                       // carta = (Sprite) mao.get(4);
                                      //  centralizaCarta(carta);
                                    }else{
                                        if ((x > (Integer) ordem.get(5))&&(x < (Integer) ordem.get(6))){
                                            return maoJogador[5];
                                           // carta = (Sprite) mao.get(5);
                                          //  centralizaCarta(carta);
                                        }else{
                                            if ((x > (Integer) ordem.get(6))&&(x < (Integer) ordem.get(7))){
                                                return maoJogador[6];
                                              //  carta = (Sprite) mao.get(6);
                                              //  centralizaCarta(carta);
                                            }else{
                                                if ((x > (Integer) ordem.get(7))&&(x < (Integer) ordem.get(8))){
                                                    return maoJogador[7];
                                                   // carta = (Sprite) mao.get(7);
                                                  //  centralizaCarta(carta);
                                                }else{
                                                    if ((x > (Integer) ordem.get(8))&&(x < (Integer) ordem.get(9))){
                                                        return maoJogador[8];
                                                      //  carta = (Sprite) mao.get(8);
                                                      //  centralizaCarta(carta);
                                                    }else{
                                                        if ((x > (Integer) ordem.get(9))&&(x < 500)){
                                                            return maoJogador[9];
                                                          //  carta = (Sprite) mao.get(9);
                                                          //  centralizaCarta(carta);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                    //carta.x = 435;
                    //carta.y = 319;
                    //carta.draw();
                }
            }
            //w.update();
            //return 0;
        }
    }

    public void centralizaCarta (int id){
        Sprite carta = (Sprite)hash.get(id);
        
        carta.x = 435;
        carta.y = 319;
        carta.draw();
    }
}
